<?php
/**
 * This file contains a logging mechanism
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: logging.class.php 72 2010-06-18 13:29:08Z sruttloff $
 * @link      phpmy
 */

/**
 * logging class
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class logging
{
    /**
     * @var object instance
     */
    protected static $INSTANCE;
    /**
    * @var string   logging type
    */
    protected $LTYPE = null;
    /**
    * @var string   name of log. destination
    */
    protected $LDESTINATION = null;
    /**
    * @var array event store
    */
    protected $LEVENT = array();
    /**
     * instantiate essential objects and make them avail. as subobjects
     *
     * @return  void
     */
    protected function __construct()
    {
    }
    /**
     * return a instance of object
     *
     * @param string $type     logging type, default file
     * @param string $filename where to store logging datas
     *
     * @return object instance
     */
    public static function getInstance($type = 'file', $filename = './logging.log')
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c ();
            self::$INSTANCE->LTYPE = strtolower($type);
            self::$INSTANCE->LDESTINATION = $filename;
        }
        return self::$INSTANCE;
    }
    /**
     * add line to log
     *
     * @param string $eventname event name of log. entry
     * @param string $text      some conent more (! only one line please)
     *
     * @return bool
     */
    public function addEvent ($eventname = null, $text = '')
    {
        $array['DateTime'] = date('Y-m-d H:i:s');
        $array['EVENT'] = $eventname . ' :: ' . $text;
        $this->LEVENT[] = implode(' > ', $array);
        return true;
    }
    /**
     * default destructor, finalize logging datas
     *
     * @return void
     * @throws Exception
     */
    public function __destruct()
    {
        try
        {
            if ($this->LTYPE == 'file')
            {
                $frontend = frontend::getInstance();;
                if ($frontend->getConfig()->get('chmod_file'))
                    $res = create_file(
                        $this->LDESTINATION, implode("\r\n", $this->LEVENT), $frontend->getConfig()->get('chmod_file')
                    );
                else
                    $res = create_file($this->LDESTINATION, implode("\r\n", $this->LEVENT));
            }
            else
            {
                /**
                 * may DB type?
                 * ToDo:
                 *
                 */

            }
            if (!$res)
                throw new Exception('write fail #' . __LINE__);
        }
        catch (Exception $e )
        {
            trigger_error($e->getMessage(), E_USER_ERROR);
        }

    }
    /**
     * store logging datas
     *
     * @return bool
     */
    public function store ()
    {
        $this->__destruct();
        return true;
    }
}
